﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 服务端双缓冲队列
{
    public class 任意进制转换
    {

        public static void Test1()
        {
            Number num = new Number(new List<string>() { "0", "1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"31",
"32",
"33",
"34",
"35",
"36",
"37",
"38",
"39",
"40",
"41",
"42",
"43",
"44",
"45",
"46",
"47",
"48",
"49",
"50",
"51",
"52",
"53",
"54",
"55",
"56",
"57",
"58",
"59",});
            long numstr = 3601;
            string str = num.ToString(numstr);
            Console.WriteLine("转换");
            Console.WriteLine(str);
            Console.WriteLine("回转");
            Console.WriteLine(num.FromString(str));
        }
        public static void Test()
        {
            Number num = new Number("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
            long numstr = 3243242343;
            string str = num.ToString(numstr);
            Console.WriteLine("转换");
            Console.WriteLine(str);
            Console.WriteLine("回转");
            Console.WriteLine(num.FromString(str));
        }
    }

    public class Number
    {
        public System.Collections.ObjectModel.ReadOnlyCollection<string> Characters
        {
            get;
            set;
        }

        public int Length
        {
            get
            {
                if (Characters != null)
                    return Characters.Count;
                else
                    return 0;
            }

        }

        public Number()
        {
            Characters = new System.Collections.ObjectModel.ReadOnlyCollection<string>("0123456789".Select(p => p + string.Empty).ToList());
        }

        public Number(string characters)
        {
            Characters = new System.Collections.ObjectModel.ReadOnlyCollection<string>(characters.Select(p => p + string.Empty).ToList());
        }

        public Number(List<string> characters)
        {
            Characters = new System.Collections.ObjectModel.ReadOnlyCollection<string>(characters.ToList());
        }


        /// <summary>  
        /// 数字转换为指定的进制形式字符串  
        /// </summary>  
        /// <param name="number"></param>  
        /// <returns></returns>  
        public string ToString(long number)
        {
            List<string> result = new List<string>();
            long t = number;

            while (t > 0)
            {
                var mod = t % Length;
                t = Math.Abs(t / Length);
                var character = Characters[Convert.ToInt32(mod)].ToString();
                result.Insert(0, character);
            }

            return string.Join("", result.ToArray());
        }

        /// <summary>  
        /// 指定字符串转换为指定进制的数字形式  
        /// </summary>  
        /// <param name="str"></param>  
        /// <returns></returns>  
        public long FromString(string str)
        {
            long result = 0;
            int j = 0;
            foreach (var ch in new string(str.ToCharArray().Reverse().ToArray()))
            {
                var strx = ch.ToString();
                if (Characters.Contains(strx))
                {
                    result += Characters.IndexOf(strx) * ((long)Math.Pow(Length, j));
                    j++;
                }
            }
            return result;
        }

    }
}
